package com.ctg.payroll2.service.workflow.imp;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.bean.ComboData;
import com.ctg.payroll2.bean.filter.WorkflowAccountFilter;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.workflow.FlowUser;
import com.ctg.payroll2.dao.workflow.WorkflowAccountDao;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.workflow.WorkflowAccountService;
import com.ctg.platform.core.security.SecurityUitls;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.Exception.SecurityException;
import com.ctg.platform.core.security.entity.Account;
import com.ctg.platform.util.Page;

public class WorkflowAccountServiceImp extends BaseService implements
		WorkflowAccountService {
	private WorkflowAccountDao dao;
	/**
	 * RBAC的service
	 */
	private SpringSecurityManager springSecurityManager;

	/**
	 * @param springSecurityManager 设置RBAC的service
	 */
	public void setSpringSecurityManager(SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}

	/**
	 * @param dao 设置dao
	 */
	public void setDao(WorkflowAccountDao dao) {
		this.dao = dao;
	}
	
	public int deleteFlowUserByIds(String ids, SysUser currentUser) throws SQLException {
		springSecurityManager.deleteAccountById(ids.split(","), currentUser);
		ids = SecurityUitls.parseIntarraytoStr(ids.split(","), ",");
		return dao.deleteFlowUserByIds(ids);
	}

	public Long insertFlowUser(FlowUser user, SysUser currentUser) throws SecurityException, SQLException {
		Account a = new Account();
		a.setDisabled("0");
		a.setUsername(user.getFlow_usercode());
		a.setPassword(user.getFlow_password());
		a = springSecurityManager.createSysAccountAndGet(a, currentUser);
		
		if (a==null || a.getId().equals("")) {
			return null;
		}
		user.setCompany_code(currentUser.getCompanyCode());
		user.setFlow_userid(a.getId());
		return dao.insertFlowUser(user);
	}

	public int updateFlowUser(FlowUser user) throws SQLException {
		return 0;
	}

	public Page queryFlowUserByFilter(WorkflowAccountFilter filter) {
		return dao.queryFlowUserByFilter(filter);
	}

	public List<ComboData> queryFlowOrgchartComb()  throws SQLException{
		return dao.queryFlowOrgchartComb();
	}
}
